package ferp.android.tasks.center;

import android.content.Context;
import android.os.AsyncTask;
import ferp.android.managers.ProfileManager;
import ferp.center.client.Action;
import ferp.center.network.RequestGameResult;
import ferp.center.network.ResponseGameResult;
import ferp.core.game.Statistics;
import ferp.core.log.Log;
import ferp.core.player.Profile;

/**
 * User: igorgok
 * Date: 5/22/12 10:48 AM
 */
public class TaskGameDelete extends AsyncTask<RequestGameResult, Void, Void>
{
  public static void execute(Context context, Profile profile)
  {
    // only if communication allowed and profile exists at the server
    // and at least one deal was played in this game
    if (profile.useCenter && !profile.hacked && profile.centerId > 0)
    {
      Statistics statistics = profile.getStatistics();
      // request to send to the server
      RequestGameResult request = new RequestGameResult();

      request.profile = profile.centerId;
      request.version = ProfileManager.instance(context).version();
      request.configuration = profile.getConfiguration();
      request.speed = -1;
      request.games = statistics.numOfGames;
      request.pool  = statistics.totalPool;
      request.cash  = statistics.totalAmount;

      new TaskGameDelete().execute(request);
    }
  }

  // dummy constructor to avoid usage mistakes
  protected TaskGameDelete() {}

  @Override
  protected Void doInBackground(RequestGameResult...request)
  {
    try
    {
      Action.execute(Action.GAME_DELETE, request[0], ResponseGameResult.class);
    }
    catch (RuntimeException e)
    {
      Log.error(Action.TAG, e);
    }
    catch (Exception e)
    {
      Log.error(Action.TAG, e);
    }

    return null;
  }
}
